﻿using KinonekoSoftware.Studio.Models.Theories;
using NLog.Targets.Wrappers;

namespace KinonekoSoftware.Studio.Pages.Theories
{
    public class MoveCatalogViewModel : ObjectDialog<PseudoCatalog>
    {
        internal const string PseudoCatalogMap = "p_map";
        internal const string PseudoCatalogs   = "p";
        internal const string TargetElement    = "t";

        private PseudoCatalog                     _targetCatalog;
        private List<PseudoCatalog>               _pseudoCatalogs;
        private Dictionary<string, PseudoCatalog> _pseudoCatalogMap;

        public MoveCatalogViewModel()
        {
            RootCatalogs   = new ObservableCollection<PseudoCatalog>();
            ParentCatalogs = new ObservableCollection<PseudoCatalog>();

            ShiftDownCommand = CreateCommand(DoShiftDownCommand);
            ShiftUpCommand   = CreateCommand(DoShiftUpCommand);
        }

        private void DoShiftUpCommand()
        {
            ParentCatalogs.ShiftUp(_targetCatalog);
            _targetCatalog.Index = ParentCatalogs.IndexOf(_targetCatalog);
        }
        
        
        private void DoShiftDownCommand()
        {
            ParentCatalogs.ShiftDown(_targetCatalog);
            _targetCatalog.Index = ParentCatalogs.IndexOf(_targetCatalog);
        }

        protected override bool OnReceive(DialogContext context)
        {
            if (!context.Has(PseudoCatalogMap) ||
                !context.Has(PseudoCatalogs))
            {
                throw new ArgumentException("缺少参数");
            }

            _pseudoCatalogMap = context.GetObject<Dictionary<string, PseudoCatalog>>(PseudoCatalogMap);
            _pseudoCatalogs   = context.GetObject<List<PseudoCatalog>>(PseudoCatalogs);
            _targetCatalog    = context.GetObject<PseudoCatalog>(TargetElement);

            RootCatalogs.Add(new PseudoCatalog
            {
                Id       = null,
                Children = new ObservableCollection<PseudoCatalog>(_pseudoCatalogs),
                Name     = "根元素 / Root",
            });

            return false;
        }

        protected override PseudoCatalog OnFinish(bool _)
        {
            return _targetCatalog;
        }


        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {

        }

        private PseudoCatalog _currentCatalog;
        private int           _indexOf;

        public int IndexOf
        {
            get => _indexOf;
            set => SetValue(ref _indexOf, value);
        }

        public PseudoCatalog CurrentCatalog
        {
            get => _currentCatalog;
            set
            {
                if (value is null)
                {
                    ParentCatalogs.Clear();
                    _targetCatalog.Index    = 0;
                    _targetCatalog.ParentID = null;
                    return;
                }


                SetValue(ref _currentCatalog, value);

                //
                // 显示当前选择节点的属性
                ParentCatalogs.AddMany(_currentCatalog.Children, true);
                _targetCatalog.Index = _currentCatalog.Children.Count;
                ParentCatalogs.Add(_targetCatalog);
                _targetCatalog.ParentID = _currentCatalog.Id;
            }
        }

        public ObservableCollection<PseudoCatalog> ParentCatalogs { get; }
        public ObservableCollection<PseudoCatalog> RootCatalogs   { get; }
        public ICommandEX                           ShiftUpCommand { get; }
        public ICommandEX                           ShiftDownCommand { get; }
    }
}